package cn.kgc.controller;

import cn.kgc.exception.exception;
import cn.kgc.pojo.*;
import cn.kgc.service.UserService;
import cn.kgc.util.Page;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @描述
 * @author：LY
 * @2018/11/8 11:56
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    /*@InitBinder// init  类加载时先执行这个方法 把字符串中变成DATE ,只对本类有效
    public void changeStringToDate(DataBinder dataBinder){
        dataBinder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy年MM月dd日"),true));
    }*/

    @Resource
    private UserService userService;

    /***
     * 登录
     * @param request
     * @return
     */
    @RequestMapping(value = "userLogin",method = RequestMethod.POST)
    public String login(String userCode, String userPassword, HttpServletRequest request){

        System.out.println(userCode+"\t"+userPassword);
        User user = userService.login(userCode,userPassword);
        if(user==null){
            request.setAttribute("error",exception.LOGIN_NOUSER);
            return "backendlogin";
        }
        request.getSession().setAttribute("userLogin",user);
        return "backend/main";
    }


    /**
     * 审核
     * @param aid
     * @param model
     * @return
     */
    @RequestMapping(value = "appcheck")
    public String appcheck(@RequestParam(value = "aid",required = false) String aid,
                           @RequestParam(value = "appid",required = false) String appid,
                           Model model) {
        UserExcesion userExcesion = userService.allQuerByID(aid);
        Version version =  userService.newVersionInfo(appid);
        model.addAttribute("appInfo",userExcesion);
        model.addAttribute("appVersion",version);
        return "backend/appcheck";
    }



    /**
     * 审核结果
     */
    @RequestMapping(value = "checksave")
    public String checksave(@RequestParam("status") String status,
                            @RequestParam("id") String id, Model model) {
        Info info = new Info();
        System.out.println(id);
        info.setId(Long.parseLong(id));
        if(status.equals("2")){
            info.setStatus(2l);
        }else if(status.equals("3")){
            info.setStatus(3l);
        }
        userService.updateByPrimaryKeySelective(info);
        return "redirect:appinfolist";
    }



    @RequestMapping(value = "applist")
    public String applist(){
        return "backend/applist";
    }




    /**
     * 总查询
     * @return
     */
    @RequestMapping(value = "appinfolist",method = RequestMethod.GET)
    public String appinfolistGet(UserExcesion userExcesion,String pageNum,Model model){
        Page<UserExcesion> list = userService.userInfo(userExcesion,pageNum);
        model.addAttribute("list",list);
        return "backend/appinfolist";
    }

    @RequestMapping(value = "appinfolist",method = RequestMethod.POST)
    public String appinfolistPost(
            @RequestParam(value = "querySoftwareName",required = false)String querySoftwareName,
            @RequestParam(value = "queryStatus",required = false)String queryStatus,
            @RequestParam(value = "queryFlatformId",required = false)String queryFlatformId,
            @RequestParam(value = "queryCategoryLevel1",required = false)String queryCategoryLevel1,
            @RequestParam(value = "queryCategoryLevel2",required = false)String queryCategoryLevel2,
            @RequestParam(value = "queryCategoryLevel3",required = false)String queryCategoryLevel3,
            @RequestParam(value = "pageIndex",required = false) String pageNum, Model model){

        model.addAttribute("querySoftwareName",querySoftwareName);
        model.addAttribute("queryStatus",queryStatus);
        model.addAttribute("queryFlatformId",queryFlatformId);
        model.addAttribute("queryCategoryLevel1",queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2",queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3",queryCategoryLevel3);


        UserExcesion userExcesion = new UserExcesion();
        userExcesion.setCategoryLevel1Name(queryCategoryLevel1);
        userExcesion.setCategoryLevel2Name(queryCategoryLevel2);
        userExcesion.setCategoryLevel3Name(queryCategoryLevel3);
        userExcesion.setFlatformIdName(queryFlatformId);
        userExcesion.setStatusName(queryStatus);
        userExcesion.setSoftwareName(querySoftwareName);

        Page<UserExcesion> list = userService.userInfo(userExcesion,pageNum);
        model.addAttribute("list",list);
        return "backend/appinfolist";
    }


    /**
     * 三级加载
     * @return
     */
    @RequestMapping("load1")
    public @ResponseBody
    Object load1(){
        List<Category> list=userService.grade();
        return JSON.toJSONString(list);
    }
    @RequestMapping("load2")
    public @ResponseBody Object load2(@RequestParam("pid")String id){
        return userService.grade2(id);
    }
    @RequestMapping("load3")
    public @ResponseBody Object load3(@RequestParam("pid")String id){
        return userService.grade3(id);
    }


    /**
     * 状态加载
     * @return
     */
    @RequestMapping("statusAjax")
    public @ResponseBody Object statusAjax(){
        return userService.statusList();
    }



    /**
     * 平台加载
     * @return
     */
    @RequestMapping("platAjax")
    public @ResponseBody Object platAjax(){
        return userService.flatfromList();
    }




}
